/*eslint-disable*/
// 兼容rem方案，不用修改

!(function(win, doc) {
  win.environment = 'test'

  // localStorage.setItem('__token__', 'cccccccccccc')
  //整个项目全局配置
  win.globalConfig = {
    //登陆url
    loginUrl: `${getEnvAndApi().baseUrl}/wx/login?callback=${btoa(
      location.href
    )}`,
    //接口地址
    apiUrl: `${getEnvAndApi().baseUrl}/api`
  }

  //获取环境信息及对应的api地址
  function getEnvAndApi() {
    var api = '//dev-api.douxuejiaoyu.com'
    switch (win.environment) {
      case 'develop':
        api = '//dev-api.douxuejiaoyu.com'
        break
      case 'test':
        api = '//dev-api.douxuejiaoyu.com'
        break
      case 'cp':
        api = '//pp-api.douxuejiaoyu.com'
        break
      case 'prod':
        api = '//api.douxuejiaoyu.com'
        break
    }

    return {
      env: win.environment,
      baseUrl: api
    }
  }

  //页面内也可以使用这个方法获取环境信息
  win.getEnvAndApi = getEnvAndApi

  //下面是设置font-size，适配rem
  function setFontSize() {
    // 获取window 宽度
    // zepto实现 $(window).width()就是这么干的
    // var winWidth =  window.innerWidth;
    var winWidth = document.body.offsetWidth
    var designWidth = 750
    doc.documentElement.style.fontSize = (winWidth / designWidth) * 100 + 'px'
  }

  var evt = 'onorientationchange' in win ? 'orientationchange' : 'resize'

  var timer = null

  win.addEventListener(
    evt,
    function() {
      clearTimeout(timer)

      timer = setTimeout(setFontSize, 300)
    },
    false
  )

  win.addEventListener(
    'pageshow',
    function(e) {
      if (e.persisted) {
        clearTimeout(timer)

        timer = setTimeout(setFontSize, 300)
      }
    },
    false
  )

  //初始化
  setFontSize()

  // localStorage存 传入为对象
  win.setLStorageObj = function(obj) {
    if (Object.prototype.toString.call(obj) !== '[object Object]') return
    const keys = Object.keys(obj)
    const avoidSameKey = '2019'
    keys.forEach(key => {
      localStorage.setItem(key + avoidSameKey, obj[key])
    })
  }

  // localStorage取 传入为数组
  win.getLStorageObj = function(arr) {
    if (Object.prototype.toString.call(arr) !== '[object Array]') return
    const result = {}
    const avoidSameKey = '2019'
    arr.forEach(key => {
      const vaule = localStorage.getItem(key + avoidSameKey)
      result[key] = vaule
    })
    return result
  }

  //判断是否是微信浏览器
  win.is_weixin = function() {
    return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1
  }
  win.getCookie = function(name) {
    var cookie = document.cookie || ''
    var arr,
      reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')

    if ((arr = cookie.match(reg))) return decodeURIComponent(arr[2])
    else return null
  }

  win.getQuery = function(query) {
    let ret = {}
    let searchReg = /([^&=?]+)=([^&]+)/g
    let name, value
    let match = searchReg.exec(query)

    while (match) {
      name = match[1]
      value = match[2]
      ret[name] = decodeURIComponent(value)
      match = searchReg.exec(query)
    }

    return ret
  }

  function juadeLogin() {
    //每个页面可能去登录，登录回来url上会多一个参数：_login_data
    var params = win.getQuery(location.href)

    if (params._login_data) {
      //解析出token，本地写入localstorage，每次请求带在请求头里
      localStorage.setItem('__token__', params._login_data)
      return
    }

    var isWeixin = win.is_weixin()
    var hasLogin = !!localStorage.getItem('__token__')

    if (!hasLogin) {
      //先隐藏界面
      document.body.style.display = 'none'
      if (isWeixin) {
        //callback - base64加密的当前url
        //url在打包的时候，会进行更换
        location.href = win.globalConfig.loginUrl
      } else {
        alert('请在微信客户端中访问本链接！')
        history.go(-1)
      }
    }
  }

  juadeLogin()

  //测试环境下，模拟退出用户，更改用户
  if (getEnvAndApi().env === 'develop') {
    let btn = document.createElement('div')
    btn.style.cssText =
      'position:fixed;right: 1rem;bottom:3rem;heigth:1rem;width:1rem;background: rgba(222, 42, 32,0.6);line-height:1rem;text-align:center;color:white;font-size:0.32rem'
    btn.innerText = '退出'
    btn.onclick = function() {
      document.cookie = ''
      location.reload()
    }
    document.body.appendChild(btn)
  }
})(window, document)

//下面是fastclick源码，不用修改
!(function() {
  'use strict'
  function t(e, o) {
    function i(t, e) {
      return function() {
        return t.apply(e, arguments)
      }
    }
    var r
    if (
      ((o = o || {}),
      (this.trackingClick = !1),
      (this.trackingClickStart = 0),
      (this.targetElement = null),
      (this.touchStartX = 0),
      (this.touchStartY = 0),
      (this.lastTouchIdentifier = 0),
      (this.touchBoundary = o.touchBoundary || 10),
      (this.layer = e),
      (this.tapDelay = o.tapDelay || 200),
      (this.tapTimeout = o.tapTimeout || 700),
      !t.notNeeded(e))
    ) {
      for (
        var a = [
            'onMouse',
            'onClick',
            'onTouchStart',
            'onTouchMove',
            'onTouchEnd',
            'onTouchCancel'
          ],
          c = this,
          s = 0,
          u = a.length;
        u > s;
        s++
      )
        c[a[s]] = i(c[a[s]], c)
      n &&
        (e.addEventListener('mouseover', this.onMouse, !0),
        e.addEventListener('mousedown', this.onMouse, !0),
        e.addEventListener('mouseup', this.onMouse, !0)),
        e.addEventListener('click', this.onClick, !0),
        e.addEventListener('touchstart', this.onTouchStart, !1),
        e.addEventListener('touchmove', this.onTouchMove, !1),
        e.addEventListener('touchend', this.onTouchEnd, !1),
        e.addEventListener('touchcancel', this.onTouchCancel, !1),
        Event.prototype.stopImmediatePropagation ||
          ((e.removeEventListener = function(t, n, o) {
            var i = Node.prototype.removeEventListener
            'click' === t
              ? i.call(e, t, n.hijacked || n, o)
              : i.call(e, t, n, o)
          }),
          (e.addEventListener = function(t, n, o) {
            var i = Node.prototype.addEventListener
            'click' === t
              ? i.call(
                  e,
                  t,
                  n.hijacked ||
                    (n.hijacked = function(t) {
                      t.propagationStopped || n(t)
                    }),
                  o
                )
              : i.call(e, t, n, o)
          })),
        'function' == typeof e.onclick &&
          ((r = e.onclick),
          e.addEventListener(
            'click',
            function(t) {
              r(t)
            },
            !1
          ),
          (e.onclick = null))
    }
  }
  var e = navigator.userAgent.indexOf('Windows Phone') >= 0,
    n = navigator.userAgent.indexOf('Android') > 0 && !e,
    o = /iP(ad|hone|od)/.test(navigator.userAgent) && !e,
    i = o && /OS 4_\d(_\d)?/.test(navigator.userAgent),
    r = o && /OS [6-7]_\d/.test(navigator.userAgent),
    a = navigator.userAgent.indexOf('BB10') > 0
  ;(t.prototype.needsClick = function(t) {
    switch (t.nodeName.toLowerCase()) {
      case 'button':
      case 'select':
      case 'textarea':
        if (t.disabled) return !0
        break
      case 'input':
        if ((o && 'file' === t.type) || t.disabled) return !0
        break
      case 'label':
      case 'iframe':
      case 'video':
        return !0
    }
    return /\bneedsclick\b/.test(t.className)
  }),
    (t.prototype.needsFocus = function(t) {
      switch (t.nodeName.toLowerCase()) {
        case 'textarea':
          return !0
        case 'select':
          return !n
        case 'input':
          switch (t.type) {
            case 'button':
            case 'checkbox':
            case 'file':
            case 'image':
            case 'radio':
            case 'submit':
              return !1
          }
          return !t.disabled && !t.readOnly
        default:
          return /\bneedsfocus\b/.test(t.className)
      }
    }),
    (t.prototype.sendClick = function(t, e) {
      var n, o
      document.activeElement &&
        document.activeElement !== t &&
        document.activeElement.blur(),
        (o = e.changedTouches[0]),
        (n = document.createEvent('MouseEvents')),
        n.initMouseEvent(
          this.determineEventType(t),
          !0,
          !0,
          window,
          1,
          o.screenX,
          o.screenY,
          o.clientX,
          o.clientY,
          !1,
          !1,
          !1,
          !1,
          0,
          null
        ),
        (n.forwardedTouchEvent = !0),
        t.dispatchEvent(n)
    }),
    (t.prototype.determineEventType = function(t) {
      return n && 'select' === t.tagName.toLowerCase() ? 'mousedown' : 'click'
    }),
    (t.prototype.focus = function(t) {
      var e
      o &&
      t.setSelectionRange &&
      0 !== t.type.indexOf('date') &&
      'time' !== t.type &&
      'month' !== t.type
        ? ((e = t.value.length), t.setSelectionRange(e, e))
        : t.focus()
    }),
    (t.prototype.updateScrollParent = function(t) {
      var e, n
      if (((e = t.fastClickScrollParent), !e || !e.contains(t))) {
        n = t
        do {
          if (n.scrollHeight > n.offsetHeight) {
            ;(e = n), (t.fastClickScrollParent = n)
            break
          }
          n = n.parentElement
        } while (n)
      }
      e && (e.fastClickLastScrollTop = e.scrollTop)
    }),
    (t.prototype.getTargetElementFromEventTarget = function(t) {
      return t.nodeType === Node.TEXT_NODE ? t.parentNode : t
    }),
    (t.prototype.onTouchStart = function(t) {
      var e, n, r
      if (t.targetTouches.length > 1) return !0
      if (
        ((e = this.getTargetElementFromEventTarget(t.target)),
        (n = t.targetTouches[0]),
        o)
      ) {
        if (((r = window.getSelection()), r.rangeCount && !r.isCollapsed))
          return !0
        if (!i) {
          if (n.identifier && n.identifier === this.lastTouchIdentifier)
            return t.preventDefault(), !1
          ;(this.lastTouchIdentifier = n.identifier), this.updateScrollParent(e)
        }
      }
      return (
        (this.trackingClick = !0),
        (this.trackingClickStart = t.timeStamp),
        (this.targetElement = e),
        (this.touchStartX = n.pageX),
        (this.touchStartY = n.pageY),
        t.timeStamp - this.lastClickTime < this.tapDelay && t.preventDefault(),
        !0
      )
    }),
    (t.prototype.touchHasMoved = function(t) {
      var e = t.changedTouches[0],
        n = this.touchBoundary
      return Math.abs(e.pageX - this.touchStartX) > n ||
        Math.abs(e.pageY - this.touchStartY) > n
        ? !0
        : !1
    }),
    (t.prototype.onTouchMove = function(t) {
      return this.trackingClick
        ? ((this.targetElement !==
            this.getTargetElementFromEventTarget(t.target) ||
            this.touchHasMoved(t)) &&
            ((this.trackingClick = !1), (this.targetElement = null)),
          !0)
        : !0
    }),
    (t.prototype.findControl = function(t) {
      return void 0 !== t.control
        ? t.control
        : t.htmlFor
        ? document.getElementById(t.htmlFor)
        : t.querySelector(
            'button, input:not([type=hidden]), keygen, meter, output, progress, select, textarea'
          )
    }),
    (t.prototype.onTouchEnd = function(t) {
      var e,
        a,
        c,
        s,
        u,
        l = this.targetElement
      if (!this.trackingClick) return !0
      if (t.timeStamp - this.lastClickTime < this.tapDelay)
        return (this.cancelNextClick = !0), !0
      if (t.timeStamp - this.trackingClickStart > this.tapTimeout) return !0
      if (
        ((this.cancelNextClick = !1),
        (this.lastClickTime = t.timeStamp),
        (a = this.trackingClickStart),
        (this.trackingClick = !1),
        (this.trackingClickStart = 0),
        r &&
          ((u = t.changedTouches[0]),
          (l =
            document.elementFromPoint(
              u.pageX - window.pageXOffset,
              u.pageY - window.pageYOffset
            ) || l),
          (l.fastClickScrollParent = this.targetElement.fastClickScrollParent)),
        (c = l.tagName.toLowerCase()),
        'label' === c)
      ) {
        if ((e = this.findControl(l))) {
          if ((this.focus(l), n)) return !1
          l = e
        }
      } else if (this.needsFocus(l))
        return t.timeStamp - a > 100 ||
          (o && window.top !== window && 'input' === c)
          ? ((this.targetElement = null), !1)
          : (this.focus(l),
            this.sendClick(l, t),
            (o && 'select' === c) ||
              ((this.targetElement = null), t.preventDefault()),
            !1)
      return o &&
        !i &&
        ((s = l.fastClickScrollParent),
        s && s.fastClickLastScrollTop !== s.scrollTop)
        ? !0
        : (this.needsClick(l) || (t.preventDefault(), this.sendClick(l, t)), !1)
    }),
    (t.prototype.onTouchCancel = function() {
      ;(this.trackingClick = !1), (this.targetElement = null)
    }),
    (t.prototype.onMouse = function(t) {
      return this.targetElement
        ? t.forwardedTouchEvent
          ? !0
          : t.cancelable &&
            (!this.needsClick(this.targetElement) || this.cancelNextClick)
          ? (t.stopImmediatePropagation
              ? t.stopImmediatePropagation()
              : (t.propagationStopped = !0),
            t.stopPropagation(),
            t.preventDefault(),
            !1)
          : !0
        : !0
    }),
    (t.prototype.onClick = function(t) {
      var e
      return this.trackingClick
        ? ((this.targetElement = null), (this.trackingClick = !1), !0)
        : 'submit' === t.target.type && 0 === t.detail
        ? !0
        : ((e = this.onMouse(t)), e || (this.targetElement = null), e)
    }),
    (t.prototype.destroy = function() {
      var t = this.layer
      n &&
        (t.removeEventListener('mouseover', this.onMouse, !0),
        t.removeEventListener('mousedown', this.onMouse, !0),
        t.removeEventListener('mouseup', this.onMouse, !0)),
        t.removeEventListener('click', this.onClick, !0),
        t.removeEventListener('touchstart', this.onTouchStart, !1),
        t.removeEventListener('touchmove', this.onTouchMove, !1),
        t.removeEventListener('touchend', this.onTouchEnd, !1),
        t.removeEventListener('touchcancel', this.onTouchCancel, !1)
    }),
    (t.notNeeded = function(t) {
      var e, o, i, r
      if ('undefined' == typeof window.ontouchstart) return !0
      if ((o = +(/Chrome\/([0-9]+)/.exec(navigator.userAgent) || [, 0])[1])) {
        if (!n) return !0
        if ((e = document.querySelector('meta[name=viewport]'))) {
          if (-1 !== e.content.indexOf('user-scalable=no')) return !0
          if (
            o > 31 &&
            document.documentElement.scrollWidth <= window.outerWidth
          )
            return !0
        }
      }
      if (
        a &&
        ((i = navigator.userAgent.match(/Version\/([0-9]*)\.([0-9]*)/)),
        i[1] >= 10 &&
          i[2] >= 3 &&
          (e = document.querySelector('meta[name=viewport]')))
      ) {
        if (-1 !== e.content.indexOf('user-scalable=no')) return !0
        if (document.documentElement.scrollWidth <= window.outerWidth) return !0
      }
      return 'none' === t.style.msTouchAction ||
        'manipulation' === t.style.touchAction
        ? !0
        : ((r = +(/Firefox\/([0-9]+)/.exec(navigator.userAgent) || [, 0])[1]),
          r >= 27 &&
          ((e = document.querySelector('meta[name=viewport]')),
          e &&
            (-1 !== e.content.indexOf('user-scalable=no') ||
              document.documentElement.scrollWidth <= window.outerWidth))
            ? !0
            : 'none' === t.style.touchAction ||
              'manipulation' === t.style.touchAction
            ? !0
            : !1)
    }),
    (t.attach = function(e, n) {
      return new t(e, n)
    }),
    'function' == typeof define && 'object' == typeof define.amd && define.amd
      ? define(function() {
          return t
        })
      : 'undefined' != typeof module && module.exports
      ? ((module.exports = t.attach), (module.exports.FastClick = t))
      : (window.FastClick = t)
})()

if (document && document.addEventListener) {
  document.addEventListener(
    'DOMContentLoaded',
    function() {
      FastClick.attach(document.body)
    },
    false
  )
}
